package Controller;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import Model.EmployeeBean;
import business.EmployeeManager;

/**
 * Servlet implementation class AdministratorController
 */
@WebServlet("/AdministratorController")
public class AdministratorController extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AdministratorController() {
        super();
        // TODO Auto-generated constructor stub
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String requestCd = null ;
		EmployeeManager employeeManager = null ;

		try {
			employeeManager = new EmployeeManager();
			// TODO: Add logic for login validation.
			requestCd = request.getParameter("REQUEST_CODE");
			System.out.println(requestCd);
			if (requestCd == null) {
				requestCd = "FETCH_EMP_SUMMARY";
			}
			//HttpSession session = request.getSession(true);
			//UserProfile userProfile = (UserProfile) session.getAttribute("USER_PROFILE");
			if (requestCd != null && !requestCd.equals("")) {
				// TODO: Consider the request comes here for only Administrator.
				// If not then this logic has to be implemented again.
				// if( userProfile != null &&
				// userProfile.getEmployeeDTO().isIs_admin()) {
				if (true) { // Remove this line and add above if authentication
							// required.
					if (requestCd.equals("FETCH_EMP_SUMMARY")) {
						ArrayList<EmployeeBean> employeeList = employeeManager.getEmployeeSummaryDetails();
						request.setAttribute("employeeList", employeeList);
						RequestDispatcher rd = request.getRequestDispatcher("/EmployeeSummary.jsp");
						rd.forward(request, response);
					} else if (requestCd.equals("UPDATE_EMPLOYEE") ) {
						int employeeId = -1;
						EmployeeBean employeeBean = new EmployeeBean();
						System.out.println(request.getParameter("EMPLOYEE_ID"));
						if (request.getParameter("EMPLOYEE_ID") != null && !request.getParameter("EMPLOYEE_ID").equals("") && !request.getParameter("EMPLOYEE_ID").equals("undefined")) {
							employeeId = Integer.parseInt(request.getParameter("EMPLOYEE_ID"));
							employeeBean.setEmployeeId(employeeId);
						} else {
							employeeBean.setEmployeeId(-1);
						}
						employeeBean.setName(request.getParameter("name"));
						employeeBean.setPassword(request.getParameter("password"));
						employeeBean.setEmail(request.getParameter("email"));
						employeeBean.setRankID(Integer.parseInt(request.getParameter("rank_id")));
						if( request.getParameter("is_administrator") != null && request.getParameter("is_administrator").equals("Y") ) {
							employeeBean.setIsAdmin(true);
						} else {
							employeeBean.setIsAdmin(false);
						}
						boolean isUpdated = employeeManager.updateEmployeeDetails(employeeBean);
						if( isUpdated ) {
							request.setAttribute("status_message","EmployeeId "+employeeBean.getEmployeeId()+" updated successfully.");
						} else {
							request.setAttribute("status_message","EmployeeId "+employeeId+" is not updated. Please check logs.");
						}
						ArrayList<EmployeeBean> employeeList = employeeManager.getEmployeeSummaryDetails();
						request.setAttribute("employeeList", employeeList);

						RequestDispatcher rd = request.getRequestDispatcher("/EmployeeSummary.jsp");
						rd.forward(request, response);
					} else if (requestCd.equals("ADD_EMPLOYEE") ) {
						RequestDispatcher rd = request.getRequestDispatcher("/EmployeeDetails.jsp");
						rd.forward(request, response);
					} else if (requestCd.equals("VIEW_EMPLOYEE") || requestCd.equals("EDIT_EMPLOYEE")) {
						int employeeId = -1;
						if (request.getParameter("EMPLOYEE_ID") != null) {
							employeeId = Integer.parseInt(request.getParameter("EMPLOYEE_ID"));
						}
						if( requestCd.equals("VIEW_EMPLOYEE")) request.setAttribute("read_only", "readonly");
						EmployeeBean EmployeeBean = employeeManager.getEmployeeDetails( employeeId);
						request.setAttribute("EmployeeBean", EmployeeBean);
						RequestDispatcher rd = request.getRequestDispatcher("/EmployeeDetails.jsp");
						rd.forward(request, response);
					} else if (requestCd.equals("DEL_EMPLOYEE")) {
						int employeeId = -1;
						if (request.getParameter("EMPLOYEE_ID") != null) {
							employeeId = Integer.parseInt(request.getParameter("EMPLOYEE_ID"));
						}
						boolean isDeleted = employeeManager.deleteEmployeeDetails( employeeId) ;
						if( isDeleted ) {
							request.setAttribute("status_message","EmployeeId "+employeeId+" deleted successfully.");
						} else {
							request.setAttribute("status_message","EmployeeId "+employeeId+" is not deleted. Either the record is already deleted or please check logs.");
						}
						ArrayList<EmployeeBean> employeeList = employeeManager.getEmployeeSummaryDetails();
						request.setAttribute("employeeList", employeeList);
						RequestDispatcher rd = request.getRequestDispatcher("/EmployeeSummary.jsp");
						rd.forward(request, response);
					} else {
						// TODO: Redirect to error page as current user is not
						// Admin.
					}
				}
			} else {
				// TODO: Redirect to error page.
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}


}
